#!/usr/bin/env python
# Author: Derek Green

PKG = 'festival'

import roslib; roslib.load_manifest(PKG)
import rospy
import subprocess

from festival.srv import FestivalSpeech

# Festival text to speech service
class SpeakText():
    def __init__(self):
        rospy.init_node('speak_text_service', anonymous=False)
        self.service = rospy.Service('speak_text', FestivalSpeech, self.handle_speak_request)

        rospy.spin()

    def handle_speak_request(self, request):
        print "SpeakText (Festival) is saying: ",request.speech_txt
        cmd = "echo \"" + request.speech_txt + "\" | /usr/bin/festival --tts"
        retval = subprocess.check_call(cmd, 0, None, None, None, None, shell=True)
        return retval

if __name__ == '__main__':
    try:
        st = SpeakText()
    except rospy.ROSInterruptException: pass
